package com.skit.zixing

import android.app.Activity
import android.app.ProgressDialog
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.skit.common.bean.StatusResult
import com.skit.common.bean.User
import com.skit.common.http.HttpUtils
import com.skit.common.ktx.RetrofitKtx.getService
import com.skit.common.utils.ToastUtils
import com.skit.common.retrofit.UserProtocol
import retrofit2.Call
import retrofit2.Retrofit

object ScanLoginAlertDialog {
    @JvmStatic
    fun show(context: Activity, retrofit: Retrofit, uuid: String, user: User) {
        AlertDialog.Builder(context).setTitle("扫码登录")
            .setMessage("是否确认登陆")
            .setNegativeButton("取消", null)
            .setPositiveButton("确认") { dialog: DialogInterface?, which: Int ->
                val progressDialog =
                    ProgressDialog(context)
                progressDialog.setMessage("正在登录")
                progressDialog.setCancelable(false)
                progressDialog.show()
                val userProtocol = retrofit.getService(UserProtocol::class.java)
                val call: Call<StatusResult<User>> =
                    userProtocol.loginForUUID(uuid, user.token)
                HttpUtils.doRequest(call) { result ->
                    progressDialog.dismiss()
                    if (result == null || !result.isSuccess) {
                        if (result != null) ToastUtils.showToast(result.msg)
                        return@doRequest
                    }
                    if (result.data != null) {
                        ToastUtils.showToast("登录成功")
                        context.finish()
                    }
                }
//                doRequest<User>(
//                    call,
//                    object : RequestFinishCallback<User> {
//                        override fun getRequest(result: StatusResult<User>?) {
//                            progressDialog.dismiss()
//                            if (result == null || !result.isSuccess) {
//                                if (result != null) ToastUtils.showToast(result.msg)
//                                return
//                            }
//                            if (result.data != null) {
//                                ToastUtils.showToast("登录成功")
//                                context.finish()
//                            }
//                        }
//                    })
            }.show()
    }
}